﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pasys.PresentationCore;
using System.Windows.Forms;
using UIShell.OSGi;
using GeneratorLib.DB;
using GeneratorLib.DB.Schemas;
using Pasys.PresentationCore.DockShell;
using Pasys.PresentationCore.DockShell.SmartPartInfos;

namespace DBExplorer
{
    public class FrmExplorerProvider : AbstractViewProvider
    {
        //public static Type ControlType
        //{
        //    get
        //    {
        //        return typeof(FrmExplorer);
        //    }

        //}

        //private static FrmExplorerProvider _defaultInstance = new FrmExplorerProvider();
        //public static FrmExplorerProvider DefaultInstance
        //{
        //    get
        //    {
        //        return _defaultInstance;
        //    }
        //}


        public bool IsShowing { get; set; }

        IDBExplorerManager manager = null;
        public FrmExplorerProvider()
            : base(false)
        {
            manager = BundleRuntime.Instance.GetFirstOrDefaultService<IDBExplorerManager>();
            IsShowing = false;
        }


        protected override Control CreateControl()
        {
            var f = new FrmExplorer()
            {
                Connections = manager.ConnectionInfos
            };
            f.ConnectionChanged += (s, e) =>
            {
                manager.SetCurrentConnectionInfo(e.ConectionInfo, e.TableInfo);
            };
            f.Shown += (s, e) =>
            {
                IsShowing = true;
                //SetCommandStatus(CommandNames.ConnecteExplorer, false);
            };
            f.FormClosed += (s, e) =>
            {
                //SetCommandStatus(CommandNames.ConnecteExplorer, true);
            };
            return f;
        }

        private void SetCommandStatus(string commandName, bool enabled)
        {
            //Console.WriteLine("{0}->{1}", commandName, enabled);
            var service = BundleRuntime.Instance.GetFirstOrDefaultService<ICommandBusService>();
            if (service == null)
            {
                return;
            }
            service.PublicCommandStatus( commandName, (enabled) ? CommandStatus.Enabled : CommandStatus.Disabled);
        }


        protected override ISmartPartInfo CreateViewInfo()
        {
            //Assembly assembly = Assembly.GetExecutingAssembly();
            //System.Drawing.Icon icon = null;

            //using (var stream = assembly.GetManifestResourceStream("DBExplorer.Resources.Hyperlink16.png"))
            //{                
            //    icon = new System.Drawing.Icon (stream, 16, 16);
            //}

            return new DockPanelSmartPartInfo()
            {
                //Icon = icon,
                HideOnClose=true,
                DockingType = DockingType.TaskView,
                ShowStrategy= new DockStateShowStrategy(DockState.DockLeft)
            };
        }
    }
}
